package com.wudi.estate.controller.communitymanagement;

import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.communitymanagement.DevelopeService;
import com.wudi.estate.service.communitymanagement.PmCompanyService;
import com.wudi.estate.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@Slf4j
@SessionAttributes(value = "maps")
@Controller
@RequestMapping("/community")
public class CommunityController {
    @Autowired
    private CommunityService communityService;
    @Autowired
    private DevelopeService developeService;
    @Autowired
    private PmCompanyService pmCompanyService;
    //小区列表一览
    @RequestMapping("/listUi")
    public String listUi(ModelMap map){
        List<Map<String,Object>> coms = new ArrayList<>();
        try {
            List<Map<String,Object>> lists= communityService.selectCommunityAndBuildsAndHouses();
            map.addAttribute("lists",lists);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "community_list";
    }
    //显示添加小区页面
    @RequestMapping("/addUi")
    public String addUi(){
        return "community_add";
    }
    // 添加小区，返回到“小区列表显示”里
    @RequestMapping("/add")
    public String add(Community community, MultipartFile image){
        String fileName="";
        try {
            fileName =  FileUtils.fileUpload(image);
            community.setImg(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            communityService.insert(community);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("进入到添加逻辑");
        return "redirect:/community/listUi";
    }
    //根据id更改显示小区信息
    @RequestMapping("/editUi")
    public String editUi(ModelMap map,long id){
        try {
            Community community= communityService.selectById(id);
            map.addAttribute("community",community);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "community_edit";
    }
    //修改完返回小区一览
    @RequestMapping("/edit")
    public String edit(Community community,MultipartFile image){
        String fileName="";
        try {
            if(!image.isEmpty()) {
                System.out.println("不为空");
                fileName = FileUtils.fileUpload(image);
                community.setImg(fileName);
            }
            else {
                System.out.println("为空");
                community.setImg(communityService.selectById(community.getId()).getImg());
            }
            communityService.updateById(community);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/community/listUi";
    }
    @RequestMapping("/delete")
    public String delete(long id){
        communityService.deleteById(id);
        return "redirect:/community/listUi";
    }
}
